class TaskController < ApplicationController
layout "office", :except => [:new, :edit] 

active_scaffold :tasks do | config | 
config.list.columns = [:id, :client, :assignment_template, :date_of_acceptance, :target_date_of_completion, :employee, :status ]
config.nested.add_link("Task's Worklogs", [:worklogentries])
end

  #def index
   # list
    #render :action => 'list'
  #end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  #verify :method => :post, :only => [ :destroy, :create, :update ],
   #      :redirect_to => { :action => :list }

#  def list
 #   @assignment_pages, @assignments = paginate :assignments, :per_page => 10
  #end

  #def show
   # @assignment = Assignment.find(params[:id])
  #end

  def new
    @task = Task.new
  end

  def create
    @task = Task.new(params[:task])
    if @task.save
      flash[:notice] = 'Task was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @task = Task.find(params[:id])
  end

  def update
    @task = Task.find(params[:id])
    if @task.update_attributes(params[:task])
      flash[:notice] = 'Task was successfully updated.'
      redirect_to :action => 'list'
      #redirect_to :action => 'show', :id => @assignment
    else
      render :action => 'edit'
    end
  end

  def save_task_assigned_to_multiple_clients
    parameters = params[:task]
    client_ids = parameters[:selected_clients]
    parameters.delete(:client_group)
    parameters.delete(:selected_clients)
     
    for id in client_ids 
      client_obj = Client.find(id)
      parameters[:client_id] = id
      task = Task.new(parameters)
      task.save
    end
    
    redirect_to :action => 'list'
  end

  def assign_task_to_multiple_clients
    @client_groups = ClientGroup.find(:all)
  end

  def get_clients
    @client_group = ClientGroup.find(params[:id])
    @clients = @client_group.clients
    @client_array = Array.new
    for client in @clients
      client_name =""
    if client.status == 'P'
      client_name = client.first_name + " " + client.laat_name
    else
      client_name = client.firm_name
    end
    @client_array.push([client_name, client.id])
    end
    
    render :update do |page|
       page.replace_html 'clients', select('task', 'clients', @client_array,{}, {:size => 5, :multiple => 'true'})
    end
  end

end  
  
